import { createSlice } from '@reduxjs/toolkit';
import { storage } from '@/utils';

const initialState = {
    token: storage.getItem(storage.keys.ACCESS_TOKEN) || '',
    userInfo: storage.getItem(storage.keys.USER_INFO) || {},
    auth: storage.getItem(storage.keys.USER_AUTH) || [],
};

const auth = createSlice({
    name: 'auth',
    initialState,
    reducers: {
        login: (state, action) => {
            state.isLogin = true;
            state.userInfo = action.payload;
        },
        logout: (state) => {
            state.isLogin = false;
            state.userInfo = {};
        },
    },
});

export const { login, logout } = auth.actions;
export default auth.reducer;
